package org.elephas.webapp.frontend.component;

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.apache.wicket.validation.validator.UrlValidator;
import org.elephas.model.Comment;
import org.elephas.model.CommentStatus;
import org.elephas.model.Post;
import org.elephas.webapp.frontend.component.common.form.ShinyForm;
import org.elephas.webapp.model.DetachableCommentModel;
import org.elephas.webapp.model.DetachablePostModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Form to enter a comment. Includes a honeypot textfield ('nick') to detect
 * spam bots.
 * 
 * @author Gerolf Seitz
 * 
 */
public class CommentFormPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private DetachablePostModel postModel;

	public CommentFormPanel(String id, DetachablePostModel post) {
		super(id);
		postModel = post;
		add(new CommentForm("form", post));
	}

	@Override
	public boolean isVisible() {
		Post post = postModel.getObject();
		return CommentStatus.OPEN.equals(post.getCommentStatus());
	}

	private static class CommentForm extends ShinyForm {
		private static final long serialVersionUID = 1L;

		private static final Logger logger = LoggerFactory
				.getLogger(CommentForm.class);

		private DetachablePostModel postModel;

		public CommentForm(String id, DetachablePostModel postModel) {
			super(id, new CompoundPropertyModel(new DetachableCommentModel()));
			this.postModel = postModel;

			add(new FeedbackPanel("feedback"));
			add(newTextField("name", 30, true));
			add(newTextField("email", 40, true).add(
					EmailAddressValidator.getInstance()));
			add(newTextField("url", 30, false).add(new UrlValidator()));
			add(new TextArea("content")
					.add(StringValidator.maximumLength(1000)).setRequired(true));

			// honeypot
			add(new TextField("nick", new Model("")));
		}

		@Override
		protected void onError() {
			super.onError();
		}

		@Override
		protected void onSubmit() {
			String honeypot = get("nick").getModelObjectAsString();
			if (Strings.isEmpty(honeypot)) {
				// empty honeypot -> user appears not to be a spam bot
				Comment comment = (Comment) getModelObject();
				Post post = postModel.getObject();
				post.addComment(comment);
				post.saveComment(comment);
				post.getBlog().savePost(post);
			} else {
				logger.info("spam bot detected");
			}

			setRedirect(true);
			throw new RestartResponseException(getPage().getClass(), getPage()
					.getPageParameters());
		}

		private TextField newTextField(String id, int minLength,
				boolean isRequired) {
			return (TextField) new TextField(id).add(
					StringValidator.maximumLength(minLength)).setRequired(
					isRequired);
		}
	}
}
